package com.user.service;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.admin.dao.AdminOrderDao;
import com.admin.dao.PetDao;
import com.bean.AdminOrder;
import com.bean.OrderDetail;
import com.bean.Pet;
import com.bean.User;
import com.user.dao.OrderDetailDao;
@Service("orderDetailServiceImp")
public class OrderDetailServiceImp implements OrderDetailService {
     @Autowired
     private OrderDetailDao orderDetailDao;
     @Autowired
     private PetDao PetDao;
     @Autowired
     private AdminOrderDao adminOrderDao;
	
	public String deleteOrderDetail(OrderDetail orderDetail) {
		// TODO Auto-generated method stub
		int re=orderDetailDao.deleteOrderDetail(orderDetail);
		if(re<0) {
			return "user/error";
		}else {
			return "forward:getOrderDetail";
		}
	}

	
	public String selectOrderDetail(HttpServletRequest req, HttpSession session) {
		// TODO Auto-generated method stub
		User u=(User) session.getAttribute("user");
		if(u==null) {
			return "user/nologin";
		}else {
		List<Map<String,Object>>list=orderDetailDao.SelectOrderDetail(u.getUid());
		req.setAttribute("list", list);
		return "user/showorder";
		}
	}

	
	public String addOrderDetail(OrderDetail orderDetail, HttpSession session,Model model) {
		// TODO Auto-generated method stub
		User u=(User) session.getAttribute("user");
		if(u==null) {
			return "user/nologin";
		}else {
		orderDetail.setUid(u.getUid());
		orderDetailDao.insertOrderDetail(orderDetail);
		AdminOrder adminorder=new AdminOrder();
		adminorder.setOrderdate(orderDetail.getOrderdate());
		adminorder.setUserphone(u.getUsertel());
		adminorder.setRealname(u.getRealname());
		adminorder.setUseraddress(u.getAddress());
		Pet p=PetDao.selectById(orderDetail.getPid());
		adminorder.setOrderinfo(p.getPname());
		adminOrderDao.insertorder(adminorder);//����Ա��Ӷ���
		Pet Pet=new Pet();
		Pet.setPid(orderDetail.getPid());
		PetDao.updateAdoptstatus(Pet);
		List<Map<String,Object>>list=orderDetailDao.SelectOrderDetail(u.getUid());
		model.addAttribute("list", list);
		return "user/showorder";
		}
	}
}
